define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index' + location.search,
                    add_url: 'goods/goods/add',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    import_url: 'goods/goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'cate_id_text', title: '一级分类', operate: false},
                        {field: 'cate_id2_text', title: '二级分类', operate: false},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                       
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'store_price', title: __('Store_price'), operate: false},
                        

                        // {field: 'is_on_sale', title: '状态',searchList: {"1":"上架","0":"下架"}, formatter: Table.api.formatter.normal},
                        // {field: 'new_status', title: '新品推荐', operate: false,formatter: Controller.api.formatter.new_status},
                        // {field: 'star_status', title: __('Star_status'), operate: false,formatter: Controller.api.formatter.star_status},
                        // {field: 'index_status', title: __('Index_status'), operate: false, formatter: Controller.api.formatter.index_status},
                        
                        {field: 'createtime', title: __('Createtime'), operate: false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //根据当前屏幕高度，实时调整table表格高度，让他能出现滚动条
            $(document).ready(function() {
                // 初始化时获取屏幕高度
                var screenHeight = $(window).height();
                //console.log("屏幕高度: " + screenHeight);
                    var searchHeigh = $(".commonsearch-table").height();

                    //console.log(screenHeight,searchHeigh);
                    //有顶部tab时候多减57
                    var thisHeight = screenHeight-searchHeigh - 200;

                    //console.log("1屏幕高度已改变: " + thisHeight);

                    $(".fixed-table-body").css("height",thisHeight);

             
                // 监听窗口大小改变事件
                $(window).resize(function() {
                    // 当窗口大小改变时，重新获取屏幕高度
                    var newScreenHeight = $(window).height();
                    // var searchHeigh = 300;
                    var searchHeigh = $(".commonsearch-table").height();

                    //console.log(newScreenHeight,searchHeigh);
                    var thisHeight = newScreenHeight-searchHeigh - 200;
                    //console.log("2屏幕高度已改变: " + thisHeight);

                    $(".fixed-table-body").css("height",thisHeight);
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $('input[name="row[index_status]"]').change(function(){

                var index_status = $(this).val();
                if (index_status == 1) {
                    $('form[role=form]').data('validator').options.ignore = '';
                    $(".goods_tag").show();
                } else {
                    $('form[role=form]').data('validator').options.ignore = '#c-tag_id';
                    $(".goods_tag").hide();
                }
            });
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $('input[name="row[index_status]"]').change(function(){

                var index_status = $(this).val();
                if (index_status == 1) {
                    $('form[role=form]').data('validator').options.ignore = '';
                    $(".goods_tag").show();
                } else {
                    $('form[role=form]').data('validator').options.ignore = '#c-tag_id';
                    $(".goods_tag").hide();
                }
            });
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });

            Controller.api.bindevent();
        },
        edit_jichu: function () {
            $('input[name="row[index_status]"]').change(function(){

                var index_status = $(this).val();
                if (index_status == 1) {
                    $('form[role=form]').data('validator').options.ignore = '';
                    $(".goods_tag").show();
                } else {
                    $('form[role=form]').data('validator').options.ignore = '#c-tag_id';
                    $(".goods_tag").hide();
                }
            });
            //不验证不可见表单
            $('form[role=form]').data("validator-options", {
                ignore: ':hidden'
            });

            Controller.api.bindevent();
        },
        store_list: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url1: 'goods/goods/store_list' + location.search,
                    // add_url: 'store/store/add',
                    // edit_url: 'store/store/edit',
                    // del_url: 'store/store/del',
                    // multi_url: 'store/store/multi',
                    // import_url: 'store/store/import',
                    table: 'store',
                }
            });


            
            


            var table = $("#table");

            

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url1,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true, formatter : stateFormatter},
                        //{field: 'id', title: __('Id')},
                        {field: 'type', title: __('类型'), searchList: {"1":__('中心'),"2":__('空间')}, formatter: Table.api.formatter.normal},
                        {field: 'store_name', title: '名称', operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        //{field: 'pid_text', title: '上级名称',operate: false},
                        
                        {field: 'realname', title: __('联系人'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'tel', title: __('手机'), operate: 'LIKE'},
                    
                        
                    
                        //{field: 'avatar', title: 'Logo', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        
                        //{field: 'business_time', title: '营业时间', operate: false,formatter: Controller.api.formatter.business_time},

                        
                        //{field: 'is_ban', title: '是否解约', searchList: {"0":'正常',"1":'已解除合约'}, formatter: Table.api.formatter.normal, operate: false},
                        //{field: 'status', title: '富有开户', searchList: {"01":'失效',"02":'正常',"03":'未开户'}, formatter: Table.api.formatter.normal, operate: false},
                      
                        //{field: 'start_time', title: '合同开始日期', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'end_time', title: '合同结束日期', operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'add_time', title: '创建时间', operate: false, formatter: Table.api.formatter.datetime},
                     
                    ]
                ]
            });
            Table.api.bindevent(table);

            function stateFormatter(value, row, index) {
                //console.log(row);
                var goodsid = Config.goodsid;
                var notgoods = row.not_goods ? row.not_goods : '';
                
 
                // 使用jQuery的$.map()函数将字符串转换为数组
                var array = $.map(notgoods.split(','), function(value) {
                    return value.trim(); // 使用trim()移除可能的空格
                });

                if (!array) {
                    return {
                        // disabled : false, //设置是否可用
                        checked : true//设置选中
                    };
                }

                if ($.inArray(goodsid, array) !== -1) {
                    
                } else {
                    return {
                        // disabled : false, //设置是否可用
                        checked : true//设置选中
                    };
                }
                return value;
            }

            //提交
            $(document).on("click", ".btn-shangjia", function () {
                // 获取所有行的数据
                var allRows = $('#table').bootstrapTable('getData');
                console.log(allRows);
                // 定义一个新数组用于存放符合条件的 id 值
                var idArray = [];//不能卖的
                var idArrayYes = [];//能卖的
                // 循环遍历 allRows 数组
                allRows.forEach(function(row) {
                    //console.log(row[0]+'--'+row.id);
                    if (row[0] === undefined || row[0] == false) {
                        idArray.push(row.id);
                    } else {
                        idArrayYes.push(row.id);
                    }
                });
                //console.log(idArray);return;
                // var ids = Table.api.selectedids(table);
                // if (idArray.length == 0) {
                //     Toastr.error('请选择空间再上架');
                //     return false;
                // }
                Fast.api.ajax({
                    url:'goods/goods/setsalestore',
                    data:{ids:idArray.join(','),idsyes:idArrayYes.join(','),'goods_id':Config.goodsid}
                }, function(data, ret){
                    //成功的回调
                    table.bootstrapTable('refresh', {});
                }, function(data, ret){
                    //失败的回调
                    table.bootstrapTable('refresh', {});
                });
            });


            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                new_status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理

                    var type = 'new_status';
                    var status = value == 1 ? 0 : 1;
                    return '<a class="btn-change text-success" data-url="goods/goods/setFlag?id=' + row.id + '&status=' + status + '&type=' + type + '" data-id="' + row.id + '"><i class="fa ' + (value == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';

                },
                star_status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理

                    var type = 'star_status';
                    var status = value == 1 ? 0 : 1;
                    return '<a class="btn-change text-success" data-url="goods/goods/setFlag?id=' + row.id + '&status=' + status + '&type=' + type + '" data-id="' + row.id + '"><i class="fa ' + (value == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';

                },
                index_status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理

                    var type = 'index_status';
                    var status = value == 1 ? 0 : 1;
                    return '<a class="btn-change text-success" data-url="goods/goods/setFlag?id=' + row.id + '&status=' + status + '&type=' + type + '" data-id="' + row.id + '"><i class="fa ' + (value == 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';

                },
                business_time: function (value, row, index) {
                    var btime = "-";
                    var etime = "-";
                    if (!row.business_start_time && !row.business_end_time) {
                        return "-";
                    }
                    if (row.business_start_time) {
                        btime = row.business_start_time;
                    }
                    if (row.business_end_time) {
                        etime = row.business_end_time;
                    }
                    return btime+"至"+etime;
                },

            }
        }
    };
    return Controller;
});
